package com.fcscs.cservice.client.main.reservation;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.BaseFormDialog;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.domain.model.ReservationView;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class FlightInfoDialog extends BaseFormDialog<ReservationView>{
	private MyFormPanel formPanel;
	private static FlightInfoDialog INSTANCE;
	
	public static FlightInfoDialog getInstance(FormSubmitListener<ReservationView> listener){
		if(null == INSTANCE){
			INSTANCE = new FlightInfoDialog();
		}
		INSTANCE.setHeading(CServiceConstants.CSERVICE_CONSTANTS.flightInfo());
		return INSTANCE;
	}
	
	private FlightInfoDialog(){
		formPanel = new MyFormPanel(null);
		formPanel.setId("FlightInfoDialog_formPanel");
		add(formPanel);
		setSize(360, 280);
	}
	
	private class MyFormPanel extends FcsFormPanel<ReservationView>{
		private Label guestNameLabel;
		private AdapterField guestNameAF;
		
		private Label arrivalDateAndTimeLabel;
		private AdapterField arrivalDateAndTimeAF;
		
		private Label arrivalFlightNoLabel;
		private AdapterField arrivalFlightNoAF;
		
		private Label arrivalFlightTimeLabel;
		private AdapterField arrivalFlightTimeAF;
		
		private Label departureDateAndTimeLabel;
		private AdapterField departureDateAndTimeAF;
		
		private Label departureFlightNoLabel;
		private AdapterField departureFlightNoAF;
		
		private Label departureFlightTimeLabel;
		private AdapterField departureFlightTimeAF;

		private FormButton closeButton;
		
		protected MyFormPanel(FormSubmitListener<ReservationView> l) {
			super(l);
//			setLabelAlign(LabelAlign.LEFT);
//			setLayout(new ColumnLayout());
			setHeaderVisible(false);
			
			LayoutContainer lc = new LayoutContainer();
			lc.setId("FlightInfoDialog_lc");
			
			FormLayout layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			layout.setLabelWidth(160);
			layout.setDefaultWidth(160);
			lc.setLayout(layout);
			
			guestNameLabel = new Label();
			guestNameLabel.setId("FlightInfoDialog_guestNameLabel");
			
			guestNameAF = new AdapterField(guestNameLabel);
			guestNameAF.setId("FlightInfoDialog_guestNameAF");
			guestNameAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.guestName());
			lc.add(guestNameAF);
			
			arrivalDateAndTimeLabel = new Label();
			arrivalDateAndTimeLabel.setId("FlightInfoDialog_arrivalDateAndTimeLabel");
			
			arrivalDateAndTimeAF = new AdapterField(arrivalDateAndTimeLabel);
			arrivalDateAndTimeAF.setId("FlightInfoDialog_arrivalDateAndTimeAF");
			arrivalDateAndTimeAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.arrivalDateTime());
			lc.add(arrivalDateAndTimeAF);
			
			arrivalFlightNoLabel = new Label();
			arrivalFlightNoLabel.setId("FlightInfoDialog_arrivalFlightNoLabel");
			
			arrivalFlightNoAF = new AdapterField(arrivalFlightNoLabel);
			arrivalFlightNoAF.setId("FlightInfoDialog_arrivalFlightNoAF");
			arrivalFlightNoAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.arrivalFlightNo());
			lc.add(arrivalFlightNoAF);
			
			arrivalFlightTimeLabel = new Label();
			arrivalFlightTimeLabel.setId("FlightInfoDialog_arrivalFlightTimeLabel");
			
			arrivalFlightTimeAF = new AdapterField(arrivalFlightTimeLabel);
			arrivalFlightTimeAF.setId("FlightInfoDialog_arrivalFlightTimeAF");
			arrivalFlightTimeAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.arrivalFlightTime());
			lc.add(arrivalFlightTimeAF);
			
			departureDateAndTimeLabel = new Label();
			departureDateAndTimeLabel.setId("FlightInfoDialog_departureDateAndTimeLabel");
			
			departureDateAndTimeAF = new AdapterField(departureDateAndTimeLabel);
			departureDateAndTimeAF.setId("FlightInfoDialog_departureDateAndTimeAF");
			departureDateAndTimeAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.departureDateTime());
			lc.add(departureDateAndTimeAF);
			
			departureFlightNoLabel = new Label();
			departureFlightNoLabel.setId("FlightInfoDialog_departureFlightNoLabel");
			
			departureFlightNoAF = new AdapterField(departureFlightNoLabel);
			departureFlightNoAF.setId("FlightInfoDialog_departureFlightNoAF");
			departureFlightNoAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.departureFlightNo());
			lc.add(departureFlightNoAF);
			
			departureFlightTimeLabel = new Label();
			departureFlightTimeLabel.setId("FlightInfoDialog_departureFlightTimeLabel");
			
			departureFlightTimeAF = new AdapterField(departureFlightTimeLabel);
			departureFlightTimeAF.setId("FlightInfoDialog_departureFlightTimeAF");
			departureFlightTimeAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.departureFlightTime());
			lc.add(departureFlightTimeAF);
			
			add(lc);
			
			closeButton = new FormButton(CoreConstants.CORE_CONSTANTS.close());
			closeButton.setId("FlightInfoDialog_closeButton");
			closeButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
				@Override
				public void componentSelected(ButtonEvent ce) {
					FlightInfoDialog.this.hide();
				}
			});
			
			getButtonBar().add(closeButton);
		}

		@Override
		public void updateForm(ReservationView entity) {
			guestNameLabel.setText(entity.getGuestFirstName() + "," + entity.getGuestLastName());
			arrivalDateAndTimeLabel.setText(CServiceConstants.DATE_FORMAT3.format(entity.getArrivalDateTime()));
			arrivalFlightNoLabel.setText(entity.getArrivalFlightNo());
			arrivalFlightTimeLabel.setText(CServiceConstants.DATE_FORMAT2.format(entity.getArrivalFlightDate()));
			departureDateAndTimeLabel.setText(CServiceConstants.DATE_FORMAT3.format(entity.getDepartureDateTime()));
			departureFlightNoLabel.setText(entity.getDepartureFlightNo());
			departureFlightTimeLabel.setText(CServiceConstants.DATE_FORMAT2.format(entity.getDepartureFlightDate()));
		}

		@Override
		public void populateEntity(ReservationView entity) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void setFocus() {
			// TODO Auto-generated method stub
			
		}
		
	}

	@Override
	protected FcsFormPanel<ReservationView> getFormPanel() {
		return formPanel;
	}

}
